﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.ObjectModel;
using System.Globalization;
using Microsoft.Practices.ObjectBuilder;
using Microsoft.Practices.CompositeWeb;
using WCSFContrib.ECommerceCatalog.Product.Services;
using WCSFContrib.ECommerceCatalog.BusinessEntities;

namespace WCSFContrib.ECommerceCatalog.Product.Views
{
    public class DefaultPresenter : Presenter<IDefaultView>
    {
        private IProductController _controller;

        public DefaultPresenter([CreateNew] IProductController controller)
        {
            this._controller = controller;
        }

        public void OnProductDetailsRequested(int productId)
        {
            Item selectedProduct = _controller.GetProduct(productId);
            View.ShowProductDetails(selectedProduct);
        }

        public void OnProductsSelecting(string searchText)
        {
            SelectProducts(searchText);
        }

        private void SelectProducts(string searchText)
        {
            if (!string.IsNullOrEmpty(searchText))
            {
                ICollection<Item> items = _controller.SearchProducts(searchText);

                View.ShowProducts(items);
                View.TotalProductsCount = items.Count;
            }
            else
            {
                View.ShowProducts(new Collection<Item>());
                View.TotalProductsCount = 0;
            }
        }

        public void OnSearchTextChanged()
        {
            View.ShowCurrentPage(0);
        }

        public void OnAddProductToCart(string userName, int itemId, int quantity)
        {
            _controller.AddProductToCart(userName, itemId, quantity);
            _controller.GoCartPage();
        }
    }
}




